package com.xxg.renrenuser.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrenuser.dao.bean.ExpressAddressInfo;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 地址信息DAO
 *
 * @author xxg
 * @date 2020/12/5
 */
@Repository
public class ExpressAddressInfoDao {
    private final MongoTemplate mongoTemplate;

    public ExpressAddressInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "expressAddressInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(ExpressAddressInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.descending("userId"));
        collection.createIndex(Indexes.descending("createTime"));
        collection.createIndex(Indexes.compoundIndex(Indexes.descending("defaultAddress"),
                Indexes.descending("createTime")));
    }

    /**
     * 插入地址
     *
     * @param record 地址信息
     * @return 结果
     */
    public int insertSelective(ExpressAddressInfo record) {
        record.setCreateTime(new Date());
        mongoTemplate.insert(record);
        return 1;
    }

    /**
     * 根据地址ID更新地址信息
     *
     * @param record 新的地址
     */
    public void updateByAddressIdSelective(ExpressAddressInfo record) {
        Query query = new Query(where("addressId").is(record.getAddressId()));
        ExpressAddressInfo info = mongoTemplate.findOne(query, ExpressAddressInfo.class);
        if (info != null) {
            record.setId(info.getId());
        }
        mongoTemplate.save(record);
    }

    /**
     * 获取会员的地址列表
     *
     * @param userId 会员ID
     * @return 地址列表
     */
    public List<ExpressAddressInfo> getAddressListByUserId(String userId) {
        Query query = new Query(where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("defaultAddress"), Sort.Order.desc("createTime")));
        return mongoTemplate.find(query, ExpressAddressInfo.class);
    }

    /**
     * 根据ID删除地址
     *
     * @param addressId 地址ID
     */
    public void deleteByAddressId(String addressId) {
        Query query = new Query(where("addressId").is(addressId));
        mongoTemplate.remove(query, ExpressAddressInfo.class);
    }

    /**
     * 获取用户默认地址
     *
     * @param userId 会员ID
     * @return 默认地址
     */
    public ExpressAddressInfo getUserDefaultAddress(String userId) {
        Query query = new Query(where("userId").is(userId).and("defaultAddress").is(1));
        return mongoTemplate.findOne(query, ExpressAddressInfo.class);
    }

    /**
     * 获取用户最新地址
     *
     * @param userId 会员ID
     * @return 最新地址
     */
    public ExpressAddressInfo getUserNewestAddress(String userId) {
        Query query = new Query(where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("createTime")));
        return mongoTemplate.findOne(query, ExpressAddressInfo.class);
    }

    /**
     * 将已有的默认地址更新为非默认地址
     *
     * @param userId 会员ID
     */
    public void updateAddressByUserId(String userId) {
        Query query = new Query(where("userId").is(userId));
        Update update = new Update();
        update.set("defaultAddress", 0);
        mongoTemplate.updateMulti(query, update, ExpressAddressInfo.class);
    }
}